<template>
  <el-dialog v-model="visible" title="审核结果">
    <el-form label-width="120" :model="props?.row" :rules="rules" ref="formRef">
      <el-form-item label="审核结果" prop="result">
        <el-radio-group v-model="props.row.result">
          <el-radio :label="2" border large> 通过</el-radio>
          <el-radio :label="3" border large>不通过</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="批注">
        <el-input type="textarea" v-model="props.row.advice" maxlength="300" show-word-limit></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button>取消</el-button>
      <el-button type="primary" @click="confirm">确认</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ElMessage, FormInstance, FormRules } from "element-plus";
import { ref } from "vue";

interface Props {
  row: any;
  api?: (params: any) => Promise<any>;
  getTable?: () => Promise<any>;
}

const initData: any = {
  status: 2,
  result: 2,
  advice: ""
};

const props = ref<Props>(JSON.parse(JSON.stringify(initData)));

const visible = ref(false);

const validResult = (rule: any, value: any, callback: any) => {
  if (value !== 2 && value !== 3) return callback(new Error("请选择审核结果"));
  callback();
};

const rules: FormRules = {
  result: [{ required: true, validator: validResult }]
};

const acceptParams = (params: Props) => {
  props.value = params;
  visible.value = true;
};

const formRef = ref<FormInstance>();

const confirm = () => {
  if (!formRef.value) return;
  formRef.value.validate(async valid => {
    if (!valid) return;
    await props.value.api!(props.value.row);
    ElMessage.success("成功");
    await props.value.getTable!();
    visible.value = false;
  });
};

defineExpose({ acceptParams });
</script>

<style></style>
